//
//  MainViewController.swift
//  SwiftWB
//
//  Created by 吴志勇 on 2019/6/13.
//  Copyright © 2019 wzy. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildControllers()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        tabBar.addSubview(compseButton)
        //保存按钮尺寸
        let rect = compseButton.frame.size
        
        //计算宽度
        let width = tabBar.bounds.width / CGFloat(children.count)
        //设置按钮的位置
        
        compseButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
    }

    @objc private func compseClick(btn:UIButton){
        WZYLog(message: 11111)
    }
    
    //MARK: -内部控制方法
    private func addChildControllers(){
        
        //1.根据JSON文件穿件控制器
        guard let filePath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
            return
            
        }
        
        guard let data = NSData(contentsOfFile: filePath) else {
            return
        }
        
        do{
            
            
            let objc = try JSONSerialization.jsonObject(with: data as Data, options:.allowFragments) as!  [[String:AnyObject]]
            for dict in objc{
                let title = dict["title"] as? String
                let imgName = dict ["imageName"] as? String
                let vcName = dict["vcName"] as? String
                adddChildViewController(childControllerName: vcName, title: title, imgName: imgName)
            }
        }catch{
            //只要try对应的方法发生异常就会执行catch{}里的方法
            adddChildViewController(childControllerName: "HomeTableViewController", title: "首页", imgName: "tabbar_home")
             adddChildViewController(childControllerName: "MessageTableViewController", title: "消息", imgName: "tabbar_message_center")
             adddChildViewController(childControllerName: "NullViewController", title: "", imgName: "")
             adddChildViewController(childControllerName: "DiscoverTableViewController", title: "发现", imgName: "tabbar_discover")
             adddChildViewController(childControllerName: "ProfileTableViewController", title: "我", imgName: "tabbar_profile")
            
        }
        
        tabBar.tintColor = UIColor.orange
        
        
        
      
    }
    
    private func adddChildViewController(childControllerName : String?, title: String?, imgName:String?){
        
        //1.动态获取命名空间
        guard let name =  Bundle.main.infoDictionary!["CFBundleExecutable"] as? String else {
            return
        }
        var cls:AnyClass? = nil
        if let vcName = childControllerName {
            cls = NSClassFromString(name + "." + vcName)
        }

        guard  let typCls = cls as? UITableViewController.Type else {
            return
        }
        
        let childController = typCls.init()
        

        
//        //1.2设置子控制器的相关属性
        childController.tabBarItem.title = title
        childController.title = title
        
        if let ivName = imgName {
            if ivName.count > 0{
                childController.tabBarItem.image = UIImage(named: ivName)
                childController.tabBarItem.selectedImage = UIImage(named: ivName + "_highlighted")
            }

        }
        
        
           
       
        
       
        
        
      
        
        
        let  nav = UINavigationController(rootViewController: childController)
//
//        //1.3将子控制器添加到UITabBarController中
        addChild(nav)
    }
    
    //MARK: -懒加载
    private lazy var compseButton: UIButton = {
        () -> UIButton
        in
        let btn = UIButton(imageName: "tabbar_compose_icon_add", imageNameHighlighted: "tabbar_compose_icon_add_highlighted", backgroundImageName: "tabbar_compose_button", backgroundImageNameHighlighted: "tabbar_compose_button_highlighted")
//        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControl.State.normal)
//        btn.setImage(UIImage(named: ""), for: UIControl.State.highlighted)
//        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControl.State.normal);
//        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControl.State.highlighted)
//        btn.addTarget(self, action: #selector(compseClick(btn:)), for: .touchUpInside)
//        btn.sizeToFit()
        return btn
    }()


}
